<?php

require_once '../../includes/include_admin.php';
require_once '../../classes/az_Category.php';

run_action();


/*
 *  Run action data;
 */

function run_action() {
    if (isset($_POST['act']) && $_POST['act'] != null) {
        $action = $_POST['act'];
    } else if (isset($_GET['act']) && $_GET['act'] != null) {
        $action = $_GET['act'];
    } else {
        $action = 0;
    }
    $html = null;
    switch ($action) {
        case 1:
            $html['html'] = add_new_category();
            $html['listcat'] = get_temp_frm_add_category();
            break;
        case 2:
            $html['html'] = update_category();
            break;
        case 3:
            if (isset($_GET['catId'])) {
                delete_category($_GET['catId']);
            }
            break;
        case 4:
            active_category();
            break;
        case 5:
            delete_all_category();
            break;
        default:
            break;
    }
    /* Send as JSON */
    header("Content-Type: application/json", true);
    echo json_encode($html);
    exit();
}

/**
 * Function add new cactegory
 */
function add_new_category() {
    if (isset($_POST['catname']) && $_POST['catname'] != null) {
        $catName = trim($_POST['catname']);
        $slug = trim($_POST['cat_slug']);
        $parent = trim($_POST['cat_parent']);
        $desc = trim($_POST['description']);
        $category = new Az_Category();
        $category->create_category(null, $catName, $desc, 0, date('d/m/Y'), null, null, $slug, $parent, 1, null);
        $result = $category->add_new_category();
        if ($result) {
            return get_list_category();
        } else {
            return '<center><i>Hệ thống hiện tại đang bận không truy vấn được dữ liệu</i></center>';
        }
    }
}

/*
 * Function get list category
 */

function get_list_category() {
    global $tpl, $az_db;
    //Create list user
    $pager = new Az_Pager();
    $file_tpl = 'category/list_cat';
    $listcat = $tpl->get_main($file_tpl, 2);

    //Create a params is array to get a block in list_user.html file
    $list_cat['list_cat'] = $tpl->get_block($listcat, 'list_cat', 1);
    //Create a $sql query for get all total category 
    $sql = "SELECT COUNT(*) FROM az_category";
    $result = $az_db->executeSQL_Query($sql)->fetch_row();
    $totalRecord = $result[0];


    //Create params for pager
    $scroll_page = 5;
    $per_page = 6;
    $current_page = isset($_GET['page']) ? $_GET['page'] : 1;
    $url = 'index.php?page=';

    //Setup a page
    $pager->page_set($url, $totalRecord, $scroll_page, $per_page, $current_page);

    //Create $sql query for get record of a page
    $sql_P = "SELECT * FROM az_category ORDER BY category_id DESC LIMIT " . $pager->start . "," . $pager->per_page;
    $result_P = $az_db->executeSQL_Query($sql_P);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $catname = $rows['name'];
            $des = $rows['description'];
            $slug = $rows['slug'];
            $post = 1; //$rows['email'];
            $checkbox = '<input type="checkbox" name="catId[]" value="' . $rows['category_id'] . '"/>';
            $actions = '<a href="edit.php?catId=' . $rows['category_id'] . ' " title="Edit" class="icon-1 info-tooltip"></a>
                        <a href="action.php?act=3&catId=' . $rows['category_id'] . ' " title="Delete" class="icon-2 info-tooltip" onclick="return deletechecked();"></a>
                        <a href="action.php?act=4&catId=' . $rows['category_id'] . ' " title="Active" class="icon-3 info-tooltip"></a>';
            $html .= $tpl->get_vars($list_cat['list_cat'], array('checkbox' => $checkbox, 'name' => $catname, 'description' => $des,
                        'slug' => $slug, 'post' => $post, 'actions' => $actions));
        }
        $result_P->free();
        //Assign $html
        $html = $tpl->assign_blocks_content($listcat, array('list_cat' => $html));
        $html = $tpl->get_vars($html, array('page_par_left' => $pager->firt_page,
                    'page_left' => $pager->previous_page, 'page-info' => $pager->page_info,
                    'page-right' => $pager->next_page, 'page-far-right' => $pager->last_page));
        return $html;
    } else {
        $result_P->free();
        $html = "<center><i>Không có người sử dụng nào trong hệ thống</i></center>";
        return $html;
    }
}

/*
 * Function delete category 
 */

function delete_category($catId) {
    if ($catId != null && is_numeric($catId)) {
        $category = new Az_Category();
        $result = $category->delete_category($catId);
        if ($result) {
            header('Location: index.php');
        }
    }
}

/*
 * Function update category
 */

function update_category() {
    if (isset($_POST['catname']) && $_POST['catname'] != null && $_POST['catId'] != null) {
        $catId = $_POST['catId'];
        $catName = trim($_POST['catname']);
        $slug = trim($_POST['cat_slug']);
        $parent = trim($_POST['cat_parent']);
        $desc = trim($_POST['description']);
        $category = new Az_Category();
        $category->create_category($catId, $catName, $desc, 0, date('d/m/Y'), date('d/m/Y'), null, $slug, $parent, 1, null);
        $result = $category->update_category();
        if ($result) {
            return $result;
        } else {
            return '<center><i>Hệ thống hiện tại đang bận không truy vấn được dữ liệu</i></center>';
        }
    }
}

/*
 * Function delete all category
 */

function delete_all_category() {
    if (isset($_POST['catId']) && is_array($_POST['catId'])) {
        //header('Location: index.php');
        $category = new Az_Category();
        $count = count($_POST['catId']);
        $tmpcount = 0;
        foreach ($_POST['catId'] as $catId) {
            $result = $category->delete_category($catId);
            if ($result) {
                $tmpcount +=1;
            }
        }
        if ($count == $tmpcount) {
            header('Location: index.php');
        }
    }
}


/*
 * Function get frm add category
 */

function get_temp_frm_add_category() {
    global $az_db;
    $html = '<option value="0" selected="true">--Select a category parent--</option>';
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE `category_parent_id` = 0 ORDER BY `name` ASC ";
    $result_P = $az_db->executeSQL_Query($sql);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $html.=' <option value="' . $rows['category_id'] . '" >' . $rows['name'] . '</option>';
            $html.= get_sub_category($rows['category_id'], '&nbsp;&nbsp;');
        }
        return $html;
        $result_P->free();
    } else {
        $result_P->free();
        return null;
    }
    return $html;
}

/*
 * Get subcategory
 */

function get_sub_category($cat_id, $str) {
    global $az_db;
    $sql = "SELECT  `category_id` ,`name` , `category_parent_id` FROM  `az_category` WHERE category_parent_id = " . $cat_id;
    $result_P = $az_db->executeSQL_Query($sql);
    if ($result_P->num_rows > 0) {
        while ($rows = $result_P->fetch_array(MYSQLI_ASSOC)) {
            $html.=' <option value="' . $rows['category_id'] . '">' . $str . $rows['name'] . '</option>';
            $html.=get_sub_category($rows['category_id'], '&nbsp;&nbsp;&nbsp;&nbsp;');
        }
        $result_P->free();
    } else {
        $result_P->free();
        return null;
    }
    return $html;
}
?>
